<template>
    <h3 style="text-align: center;">理财添加</h3>
    <table class="table-bordered" border="1">
        <!-- 理财名称、年收益、利息、最低金额、理财人次、风险等级 -->
        <tr>
            <td>理财名称</td>
            <td>
                <input v-model="Info.B_Iname" type="text" placeholder="请输入理财名称">
            </td>
        </tr>
        <tr>
            <td>年收益</td>
            <td>
                <input type="text" v-model="Info.B_IAPY" placeholder="请输入年收益">
            </td>
        </tr>
        <tr>
            <td>
                利息
            </td>
            <td>
                <select v-model="Info.B_Tid">
                    <option value="0">-全部-</option>
                    <option v-for="item in XIal" :value=item.B_Tid>{{item.B_Tyear}}利息为{{item.B_Tleave}}%</option>
                </select>
            </td>
        </tr>
        <tr>
            <td>
                最低金额
            </td>
            <td>
                <input type="text" v-model="Info.B_Iminmoney" placeholder="请输入最低金额">
            </td>
        </tr>
        <tr>
            <td>
                风险等级 
            </td>
            <td>
                <select v-model="Info.B_IFX">
                    <option value="低风险">低风险</option>
                    <option value="中风险">中风险</option>
                    <option value="高风险">高风险</option>
                </select>
            </td>
        </tr>
        <tr style="text-align: center;">
            <td>
                <button class="btn btn-primary" @click="AddInfo">添加</button>
            </td>
        </tr>
    </table>
</template>
<script setup lang="ts">
import { ref } from 'vue';
import axios from 'axios';
// import { useRouter } from 'vue-router';
// const router = useRouter();
//添加信息双向绑定
const Info=ref({
    B_Iid:0,
    B_Iname:'',
    B_IAPY:'',
    B_Tid:0,
    B_Iminmoney:'',
    B_INumber:0,
    B_IFX:'',
})
const XIal=ref([{
    B_Tid:0,
    B_Tyear:0,
    B_Tleave:0,
}])
import { onMounted } from 'vue';
import router from '../../router';
onMounted(()=>{
  Bindss();  
})
//绑定下拉
const Bindss=()=>{
  axios.get("https://localhost:44356/api/Finans/Bindlx")
  .then(res=>{
    XIal.value=res.data;
  })
  .catch(err=>{
    console.log(err);
  })
}
//添加信息
const AddInfo=()=>{
    // 最低金额必须大于0
    if(Number(Info.value.B_Iminmoney)<=0){
      alert("最低金额必须大于0");
      return;
    }
   
  axios.post("https://localhost:44356/api/Finans/AddFinanInfo",
  {
    B_Iid:Info.value.B_Iid,
    B_Iname:Info.value.B_Iname,
    B_IAPY:Info.value.B_IAPY,
    B_Tid:Info.value.B_Tid,
    B_Iminmoney:Info.value.B_Iminmoney,
    B_INumber:Info.value.B_INumber,
    B_IFX:Info.value.B_IFX,
  })
  .then(res=>{
    if(res.data>0)
    {
      alert("添加成功");
      router.push("/ShowFinanInfo");
    }
    else if(res.data==-1)
    {
        alert("理财项目名称不能重复")
    }
    else{
      alert("添加失败");
    }
    console.log(res);
  })
}
</script>